package com.example.gate.listener;

import com.example.common.constants.ServiceNames;
import com.example.common.grpc.ServiceInstanceRegistrar;
import com.example.common.listener.AbstractServiceChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;

/**
 * 服务变更监听器
 * 监听Nacos服务实例的变化，特别是lobby-service的下线事件
 */
@Slf4j
@Component
public class ServiceChangeListener extends AbstractServiceChangeListener {

    public ServiceChangeListener(DiscoveryClient discoveryClient, ServiceInstanceRegistrar serviceInstanceRegistrar) {
        super(discoveryClient,serviceInstanceRegistrar);
    }
    @Override
    protected void handleServiceChange() {
        // 监听所有服务变化，但只处理lobby-service
        handleServiceChange(ServiceNames.LOBBY);
        handleServiceChange(ServiceNames.GAME);
    }

    @Override
    protected void onServiceInstanceChange(String serviceName, java.util.List<org.springframework.cloud.client.ServiceInstance> currentInstances, java.util.List<org.springframework.cloud.client.ServiceInstance> lastInstances) {
    }

    /**
     * 获取当前lobby-service可用实例数
     */
    public int getCurrentLobbyInstanceCount() {
        return getCurrentInstanceCount(ServiceNames.LOBBY);
    }

    /**
     * 检查lobby-service是否可用
     */
    public boolean isLobbyServiceAvailable() {
        return isServiceAvailable(ServiceNames.LOBBY);
    }
} 